package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.RecoverConst;
import cn.stylefeng.guns.modular.business.entity.PinChannel;
import cn.stylefeng.guns.modular.business.mapper.PinChannelMapper;
import cn.stylefeng.guns.modular.business.model.params.PinChannelParam;
import cn.stylefeng.guns.modular.business.model.result.PinChannelResult;
import  cn.stylefeng.guns.modular.business.service.PinChannelService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 销卡渠道 服务实现类
 * </p>
 *
 * @author jwj
 * @since 2019-08-15
 */
@Service
public class PinChannelServiceImpl extends ServiceImpl<PinChannelMapper, PinChannel> implements PinChannelService {

    @Override
    public void add(PinChannelParam param){
        PinChannel entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(PinChannelParam param){
        PinChannel channel = new PinChannel();
        channel.setPinChannelId(param.getPinChannelId());
        channel.setDeleteFlag(RecoverConst.DEL_FLAG_TRUE);
        updateById(channel);
    }

    @Override
    public void update(PinChannelParam param){
        PinChannel oldEntity = getOldEntity(param);
        PinChannel newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public PinChannelResult findBySpec(PinChannelParam param){
        return null;
    }

    @Override
    public List<PinChannelResult> findListBySpec(PinChannelParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(PinChannelParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 更新状态
     *
     * @param param
     * @author jwj
     * @Date 2019-07-12
     */
    @Override
    public void status(PinChannelParam param) {
        PinChannel pinChannel = new PinChannel();
        pinChannel.setPinChannelId(param.getPinChannelId());
        pinChannel.setStatus(param.getStatus());
        this.updateById(pinChannel);
    }

    private Serializable getKey(PinChannelParam param){
        return param.getPinChannelId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private PinChannel getOldEntity(PinChannelParam param) {
        return this.getById(getKey(param));
    }

    private PinChannel getEntity(PinChannelParam param) {
        PinChannel entity = new PinChannel();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
